package com.woniu.message.listener;


import com.ruoyi.common.constant.UserConstants;
import com.woniu.message.feign.UserFeign;
import com.woniu.user.domain.CustomerCoupon;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
@Component
public class CouponCustomerListener {
    @Autowired
    private UserFeign userFeign;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RabbitListener(queues = UserConstants.DLX_CUSTOMER_COUPON_QUEUE)
    public void updateCouponStatus(Long add){
        CustomerCoupon customerCoupon = userFeign.selectOneById(add);
        if (!customerCoupon.getValidEndDate().after(new Date())){
            userFeign.edit(add);
        }else {
            rabbitTemplate.convertAndSend(UserConstants.DELAY_CUSTOMER_COUPON_EXCHANGE,
                                          UserConstants.DELAY_CUSTOMER_COUPON_KEY,
                                          add);
        }
    }
}
